package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Activity;
import com.qk.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/activities")
@Slf4j
@RestController
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    /**
     * 获取活动列表
     */
    @GetMapping
    public Result listActivity(Integer channel,Integer type,Integer  status,
                               @RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("channel:{},type:{},status:{},page:{},pageSize:{}",channel,type,status,page,pageSize);
        PageResult<Activity> activity= activityService.listActivity(channel,type,status,page,pageSize);
        return Result.success(activity);
    }

    /**
     * 新增活动信息
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Activity activity) {
        log.info("activity:{}",activity);
        activityService.add(activity);
        return Result.success();
    }

    /**
     * 根据id查询回显
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("id:{}",id);
        Activity activity = activityService.getById(id);
        return Result.success(activity);
    }

    /**
     * 修改活动信息
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Activity activity) {
        log.info("activity:{}",activity);
        activityService.update(activity);
        return Result.success();
    }

    /**
     * 删除活动信息
     */
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("id:{}",id);
        activityService.deleteById(id);
        return Result.success();
    }

    /**
     * 查询指定类型的活动
     */
    @GetMapping("/type/{type}")
    public Result list(@PathVariable Integer type) {
        log.info("type:{}",type);
        List<Activity> list = activityService.list(type);
        return Result.success(list);
    }
}
